package edu.gatech.spacetraderteam3.view;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 * A class that represents the button widget on screen
 * @author Alexander Ikonomidis
 * @version 1.0
 */
public class SGGButton extends SGGComponent {
    
	/**
	 * Boolean values for whether the button is pressed or if the
	 * background should be shown
	 */
	private boolean pressed = false, drawBackground = true;

	/**
	 * The button's up and down images
	 */
    private BufferedImage buttonDown, buttonUp;
    
    /**
     * The button's type (for images and execution sake)
     */
    private Assets.ButtonType buttonType;

    /**
     * The text displayed on the button
     */
    private String text = null;

    /**
     * Constructor for fancy Buttons. Sets image asset and bounds
     * @param buttonType
     */
    public SGGButton(Assets.ButtonType buttonType) { // if true, string is filename
        super(0, 0);
        this.buttonType = buttonType;
        buttonUp = buttonType.images()[0];
        buttonDown = buttonType.images()[1];
        setDimensions(buttonUp.getWidth(), buttonUp.getHeight());
    }

    /**
     * Constructor for basic text Buttons. Sets text of the buttons and default button assets 
     * @param text
     */
    public SGGButton(String text) {
        this(Assets.ButtonType.BASIC);
        this.text = text;
    }

    /**
     * Setter for the Button's text
     * @param text Text the button will display
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Sets whether to draw the background
     * @param drawBackground whether to draw the background
     */
    public void drawBackground(boolean drawBackground) {
        this.drawBackground = drawBackground;
        if (drawBackground) {
            setDimensions(buttonUp.getWidth(), buttonUp.getHeight());
        } else {
            if (text == null) {
                text = "";
            }
        }
    }

    /**
     * Paint method for Buttons. Draws backgrounds, if set to, and text, if set to. 
     * @param g Graphics component to draw onto
     */
    public void doPaint(Graphics2D g) {
        if (drawBackground) {
            g.drawImage((pressed ? buttonDown : buttonUp), getX(), getY(), null);
        } else {
            final int wid = g.getFontMetrics().stringWidth(text);
            final int hei = g.getFontMetrics().getHeight();
            setDimensions(wid, hei);
        }
        if (text != null) {
            g.setColor((pressed ? color.darker() : color));
            final int wid = g.getFontMetrics().stringWidth(text);
            final int hei = g.getFontMetrics().getHeight();
            g.drawString(text, getX() + getWidth() / 2 - wid / 2, getY() + getHeight()
                    / 2 + hei / 4);
        }
    }

    /**
     * Getter for whether the button is pressed
     * @return Whether button is pressed 
     */
    public boolean isPressed() {
        return pressed;
    }

    /**
     * Sets pressed to true and repaints
     * @param p Point that was clicked
     */
    protected void doClick(Point p) {
        if (!pressed) {
            pressed = true;
            repaint();
        }
    }

    /**
     * Sets pressed to false, calls the button's associated event and repaints
     */
    protected void doRelease() {
        pressed = false;
        sggEventListener.doEvent(new SGGEvent(buttonType.toString(), this));
        repaint();
    }

    /**
     * @return "Button" + the text on the button.
     */
    public String toString() {
        return "Button: " + text;
    }
}
